/**
 * loadTreeTable
 */
function loadTreeTable() {
    $("#treeTable").treeTable({
        url: base + "/resource/view/list",
        column: 1,
        expandable: true,
        theme: "default",
        columns: [
            {field: "name", title: "名称", algin: "center",width: "150"},
            {
                field: "icon",  title: "图标", formatter: function (value) {
                return tools.format("-1" == value.parentId ? "" : '<i class="{0}"></i>',value.icon);
            }
            },
            {
                field: 'sort',  title: "排序", width: "70", formatter: function (value) {
                   var html = '<input onchange="updateSort(\'' + value.id + '\',this)" value="' + value.sort + '" class="form-control input-sm" maxlength="3"/>';
                   return html;
                }
            },
            {
                field: 'type', title: "类型", width: "70", formatter: function (value) {
                var text = {"0": "文件夹", "1": "菜单", "2": "动作"};
                return text[value.type];
            }
            },
            {field: "url", title: "跳转地址"},
            {
                field: "isEnabled", title: "状态", algin: "center", formatter: function (value) {
                     return value.isEnabled=='1'? "启用" : "禁用"
                }
            },
            {field: "description", title: "描述", algin: "right"},
            {
                title: "操作", align: "center", width: "250", formatter: function (value) {
                var html = '<div class="btn-group">';
                var pid = value.parentId;
                if ("-1" == pid) {
                    html = tools.format('<a class="btn btn-sm btn-primary" href="{0}/resource/sk/toAdd?parentId={1}">添加下级</a>',base, value.id);
                } else {
                    html += tools.format('<a class="btn btn-sm purple " href="{0}/resource/uc/toEdit?id={1}">  编辑  </a>',base, value.id);
                    html += tools.format('<a class="btn btn-sm btn-danger" onclick="delResource(\'{0}\')">  删除  </a>',value.id);
                    html += tools.format('<a class="btn btn-sm btn-primary " href="{0}/resource/sk/toAdd?parentId={1}">添加下级</a>',base, value.id);
                }

                html += '</div>';
                return html;
            }
            }
        ]
    });
}

function doSubmit(type) {
    if (!formCheck().form()) {
        return;
    }
    var url;
    if ("add" == type) {
        url = base + "/resource/sk/insert";
    } else {
        url = base + "/resource/uc/update";
    }
    $.post(url, $("#editForm").serialize(), function (data) {
        $.messager.alert0({selector: "#editAlert", type: data.code, html: data.message});
        if (data && data.code == 'SUCCESS') {
            tools.goto(base + "/resource/view/index", 500);
        }
    }, "json");
}

function formCheck() {
    return $('#editForm').validate({
        rules: {
            name: {
                maxlength:[24],
                required: true
            },
            sort: {
                maxlength:[4],
                digits:true,
                required: true
            },
            resName: {
                required: true
            },
            appName: {
                required: true
            },
            type: {
                required: true
            }
        },
        messages:{
            type:{
                required:"请选择类型"
            }
        }
    });
}


/**
 * switchButton
 */
function switchButton() {
    $('.btn-group > button').click(function () {
        var btn = $(this);
        btn.parent().find('button').removeClass('btn-success');
        btn.addClass('btn-success');
        btn.parent().find('input').val(btn.val());
    });
}

/**
 * 删除资源
 */
function delResource(id) {
	if (!id) {
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    var childs = $('#' + id).attr("hasChild");
    if ("true" == childs) {
        $.messager.alert("请先删除下级数据");
        return;
    }
	
    $.messager.confirm("您确定要删除记录吗", function (res) {
        if (res) {
            $.post(base + "/resource/dh/del", {id: id}, function (data) {
                $.messager.alert0({selector: "#messageInfo", type: data.code, html: data.message});
                if ('SUCCESS' == data.code) {
                    $('#' + id).remove();
                    //判断是否修改上级节点图标
                    if ($("tr[pid=" + parentId + "]").size() == 0) {
                        $("tr[id=" + parentId + "]").find(".default_active_node").remove();
                    }
                }
            }, "json");
        }
    });

}
/**
 * 更新排序
 * @param id
 * @param curr
 */
function updateSort(id, curr) {
    var sort = $(curr).val();
    $.ajax({
        type: "POST",
        url: base + "/resource/uc/updateSort",
        data: {id: id, sort: sort},
        dataType: "json",
        success: function (data) {
            if ('SUCCESS' != data.code) {
                $.messager.alert(data.message);
            }
        }
    });
}

